/**
@author - Gyanendra Singh/ Suresh Chandra Pal
@version - 1.0 28/Feb/2009
**/
/////////////////////
// Email validation code
/////////////////////

function emailValidation(list) {		
	var  email = list.value;
	if (email.length > 50) {	
		return false;
	}
	if (! (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(list.value))) {
		return false;
	}
	var i = email.indexOf('@');

	if(i < 2) {
		return false;
	}
	
	var username = email.substring(0 , i);
	var domain = email.substring(i + 1, email.length);
	i=0;
	while((username.substring(i , i + 1) == " ") && (i < username.length)) i++;

	if(i > 0) username = username.substring(i, username.length);
	
	i = domain.length - 1;
	while ((domain.substring(i, i + 1) == " ") && (i >= 0)) i-- ;
	if(i < (domain.length - 1)) domain = domain.substring(0, i + 1);

	var ch;
	for (i = 0; i < username.length; i++) {
		ch = (username.substring(i, i + 1)).toLowerCase();
		if(i==0) {
			if(!(((ch >= "a") && (ch <= "z")) || ((ch >= "0") && (ch <= "9")))) {		
				return false;
			}
		} else {
			if (!(((ch >= "a") && (ch <= "z")) || ((ch >= "0") && (ch <= "9")) ||(ch == "_") || (ch == "-") || (ch == "."))) {					
				return false;
			}
		}
	}

	for (i = 0; i < domain.length; i++) {
		ch = (domain.substring(i, i + 1)).toLowerCase();
		if(i==0) {
			if(!(((ch >= "a") && (ch <= "z")) || ((ch >= "0") && (ch <= "9")))) {			    	
			    return false;
			}
		} else {
			if (!(((ch >= "a") && (ch <= "z")) || ((ch >= "0") && (ch <= "9")) ||(ch == "_") || (ch == "-") || (ch == "."))) {		
			    return false;
			}
		}
	}

	var subdomainarray = new Array ();
	subdomainarray = [ ".com", ".edu", ".us", ".gov", ".in", ".mil", ".net", ".org", ".info", ".biz", ".name", ".pro", ".museum", ".coop", ".aero", ".org"];
	i=0;
	var subdomain = domain.substring(domain.lastIndexOf(".") , domain.length);
	for(i = subdomainarray.length -1 ; i >= 0 ; i--) {
		if((subdomain == subdomainarray [i])) {				
			i=-1;
		}
	}
	if(i == -1) {			
		return false;
	}		
	return true;
}	


//////////////////////////
// Zip validation code
//////////////////////////

function zipCodeValidation(list) {
	var zip = list.value;
	var i = zip.length;
	if(i == 5) {
		var ch;
		for (i = 0; i < zip.length; i++) {
			ch = zip.substring(i , i +1);
			if(!((ch >= "0") && (ch <= "9"))) {
				return false;
			}
		}
		if(zip.substring(0 , 5) == '00000') {				
			return false;
		}
	} else {
		if(i == 10) {
			if(zip.indexOf("-") != '5') {
				return false;
			}
			
			var ch;
			for (i = 0; i < 5; i++) {
				ch = zip.substring(i , i +1);
				if(!((ch >= "0") && (ch <= "9"))) {						
					return false;
				}
			}
			for (i = 6; i < 10; i++) {
				ch = zip.substring(i , i +1);
				if(!((ch >= "0") && (ch <= "9"))) {						
					return false;
				}
			}
			if((zip.substring(0 , 5) == '00000') && (zip.substring(6 , 10) == '0000')) {					
				return false;
			}
		} else {				
			return false;
		}
	}
	return true;
}


////////////////////////////
// Phone number Validation
////////////////////////////

function phoneNumberValidation(list) {
	var phone = list.value;
	var i = phone.length;
	if( i != 10) {			
		return false;
	}
	var ch;
	for (i = 0; i < phone.length; i++) {
		ch = phone.substring(i , i +1);
		if(!((ch >= "0") && (ch <= "9"))) {				
			return false;
		}
	}
	if(((phone.substring(0 , 3)) == '000') || (phone.charAt(0) == '1') || (phone.charAt(0) == '0')) {			
		return false;
	}
	if(phone.substring(3 , 10) == '0000000') {			
		return false;
	}
	return true;
}

///////////////////////
// Extention validation
////////////////////////

function extensionValidation (list) {
	var extension = list.value;
	var i = extension.length;
	var ch;
	for (i = 0; i < extension.length; i++) {
		ch = extension.substring(i , i +1);
		if(!((ch >= "0") && (ch <= "9"))) {				
			return false;
		}
	}
	return true;
}


///////////////////////
// Integer validation
////////////////////////

function integerValidation (list) {
	var intCheck = list.value;
	var i = intCheck.length;
	var ch;
	for (i = 0; i < intCheck.length; i++) {
		ch = intCheck.substring(i , i +1);
		if(!((ch >= "0") && (ch <= "9"))) {				
			return false;
		}
	}
	return true;
}


///////////////////////////
//Pager validation
///////////////////////////

function pagerValidation (list) {
	var name = list.value;
	var ch;
	var i;
	for (i = 0; i < name.length; i++) {
		ch = (name.substring(i, i + 1)).toLowerCase();		
		if(!( ((ch >= "a") && (ch <= "z")) || ((ch >= "0") && (ch <= "9")))) {		
			return false;
		}
	}	
	return true;
}


///////////////////////////
//Name validation
///////////////////////////

function alphaNameValidation (list) {
	var name = list.value;
	var ch;
	var i;
	
	var ct;

	if (name.charAt(0) == "'") {
		return false;
	}

	for (i = 0; i < name.length; i++) {
		var k = i;
		if ( (name.charAt(k) == "'") && (name.charAt(k+1)== "'") ) {
			return false;
		}
	}

	if ((name.length == name.lastIndexOf("'")+1 )) {
 		return false ;
 	}
	
	for (i = 0; i < name.length; i++) {
		ch = (name.substring(i, i + 1)).toLowerCase();		
		if(!( ((ch >= "a") && (ch <= "z")) || (ch == "'") || (ch == " ")) ) {		
			return false;
		}
	}	
	return true;
}

function alphNumericNameValidation (list) {	
	var name = list.value;
	var ch;
	var i;
	var ct;

	if (name.charAt(0) == "'") {
		return false;
	}

	for (i = 0; i < name.length; i++) {
		var k = i;
		if ( (name.charAt(k) == "'") && (name.charAt(k+1)== "'") ) {
			return false;
		}
	}

	if ((name.length == name.lastIndexOf("'")+1 ) || (name.length == name.lastIndexOf("&")+1 ) || ( name.length == name.lastIndexOf("-")+1 ) || ( name.length == name.lastIndexOf("_")+1 ) ||  ( name.length == name.lastIndexOf(".")+1 )  ) {
 		return false ;
 	}

	for (i = 0; i < name.length; i++) {
		ch = (name.substring(i, i + 1)).toLowerCase();		
		if(!( ((ch >= "a") && (ch <= "z")) || ((ch >= "0") && (ch <= "9")) || (ch == "'") || (ch == " ") || (ch == "_") || (ch == ".")  || (ch == "-")) ) {			
			return false;
		}
	}	
		return true;
}	

////////////////////////////////////////////////
//Date  validation (yyyy-mm-dd)
///////////////////////////////////////////////
function dateValidation(list){
	var dateTime = list.value;
	var date = dateTime.substring(0, dateTime.length);

	var validDateFormat=/^\d{4}\-\d{2}\-\d{2}$/ ;
	if (!validDateFormat.test(date)) {
		return false;
	} else{ 
		var yearfield=date.split("-")[0]
		var monthfield=date.split("-")[1]
		var dayfield=date.split("-")[2]
		var dayobj = new Date(yearfield, monthfield-1, dayfield)
		if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield)) { 
			return false;
		}
	}
}

/////////////////////////////////
//alphaNumeric validation
/////////////////////////////////

function alphaNumericValidation(list) {
	var numaric = list.value;
	for(var j=0; j<numaric.length; j++) {
		var alphaa = numaric.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if( ! ((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123)) ) {
		  	return false;
		} 		
	}
 	return true;
}

///////////////////////////
//Manufacture  validation
///////////////////////////

function userNameValidation(list) {
	var name = list.value;
	var ch;
 	var i;
 	for (i = 0; i < name.length; i++) {
  		ch = (name.substring(i, i + 1)).toLowerCase();  
  		if(!( ((ch >= "a") && (ch <= "z")) || ((ch >= "0") && (ch <= "9")) || (ch == "_") || (ch == " ") || (ch == "-") || (ch == ".") || (ch == "&") || (ch == "#")) ) {  
   			return false;
  		}
 	} 
 	if ((name.length == name.lastIndexOf("_")+1 ) || (name.length == name.lastIndexOf(".")+1 ) || ( name.length == name.lastIndexOf("&")+1 ) || ( name.length == name.lastIndexOf("-")+1 ) || ( name.length == name.lastIndexOf("#")+1 ) ) {
 		return false ;
 	}
  	return true;
}

////////////////////////////////////
// Phone number Validation (landline)
////////////////////////////////////
function CheckPhoneNumber(PhoneNo) {

 	// get rid of non-numerics
 	var checkOK = new String("0123456789");
 	var strNo = new String(PhoneNo.value);
 	PhoneNo.value="";

 	for ( i = 0; i < strNo.length; i++ ) {
  		ch = strNo.charAt(i);
  		for (j = 0;  j < checkOK.length;  j++)
   			if (ch == checkOK.charAt(j))
    			break;

  		if (j != checkOK.length) {
   			PhoneNo.value=String(PhoneNo.value)+String(ch);
  		}
 	}

 	// get rid of leading 1
 	if ( ( PhoneNo.value.length == 11 ) && ( PhoneNo.value.charAt(0) == 1 ) ) {
  		PhoneNo.value = PhoneNo.value.substr(1,10);
 	}

 	if ( (PhoneNo.value.length != 0) && (PhoneNo.value.length < 10) ) {
  		return (false);
 	}

 	if (PhoneNo.value.length > 10) {
  		return (false);
 	}

 	var checkStr = PhoneNo.value;
 	var allValid = true;
 	var decPoints = 0;

 	for (i = 0;  i < checkStr.length;  i++) {
  		ch = checkStr.charAt(i);
  		for (j = 0;  j < checkOK.length;  j++)
   			if (ch == checkOK.charAt(j))
    			break;

  		if (j == checkOK.length) {
   			allValid = false;
   			break;
  		}
 	}

 	if (!allValid) {
  		return (false);
 	}

 	if ( checkStr.length == 10 ) {
  		if ( checkStr.charAt(0) < '2' ) {
   		return (false);
  	}

  	if ( checkStr.charAt(3) < '2' ) {
  	 	return (false);
  	}

  	if ( checkStr.substring(0,3) == "900" ) {
   		return (false);
  	}
 } else
  return (true);
}

/////////////////////////////// 
////Validation For Strings
/////////////////////////////
function deleteString (stringValue){
	var stringName = stringValue;		
	var	stringFirst = "";	
	var stringList = "";
	
	while(stringName.length > 0) {
		if(stringName.indexOf("#") > 0) {
			stringFirst = stringName.substring(0,stringName.indexOf("#"));
			stringName = stringName.substring(stringName.indexOf("#")+1,stringName.length);
		} else {
			stringFirst = stringName;
			stringName = "";
		}
		stringList = stringList + stringFirst + "'";
	}
	
	stringList = stringList.substring(0 , stringList.length -1);
	return  stringList;
}

//Method used to trim the space.
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

/////////////////////////
// Fee validation
/////////////////////////

function feeValidation(list) {
	var price = list.value;
	var i = price.indexOf('.');
	var ch;
	if (i >= 1) {
		var beforeDecimal = price.substring(0 , i);
		var afterDecimal = price.substring(i + 1, price.length);
		if(afterDecimal.length==0) {
			return false;
		}
		
		for (i = 0; i < beforeDecimal.length; i++) {
			ch = (beforeDecimal.substring(i, i + 1))
			if(! ((ch >= "0") && (ch <= "9")) ) {		
				return false;
			}
		}

		for (i = 0; i < afterDecimal.length; i++) {
			ch = (afterDecimal.substring(i, i + 1))
			if(! ((ch >= "0") && (ch <= "9")) ) {		
				return false;
			}
		}
	} else {
		for (i = 0; i < price.length; i++) {
			ch = (price.substring(i, i + 1))
			if(! ((ch >= "0") && (ch <= "9")) ) {		
				return false;
			}
		}
	}
	return true;
}
